package com.bdmc.task.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.bdmc.common.core.page.TableDataInfo;
import com.bdmc.common.core.domain.PageQuery;
import com.bdmc.task.domain.TaskTeamSpan;
import com.bdmc.task.domain.bo.TaskTeamSpanBo;
import com.bdmc.task.service.ITaskTeamSpanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.bdmc.common.core.mybatisplus.core.ServicePlusImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.bdmc.common.utils.StringUtils;
import com.bdmc.task.domain.bo.TaskWorkTeamBo;
import com.bdmc.task.domain.vo.TaskWorkTeamVo;
import com.bdmc.task.domain.TaskWorkTeam;
import com.bdmc.task.mapper.TaskWorkTeamMapper;
import com.bdmc.task.service.ITaskWorkTeamService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 作业班组Service业务层处理
 *
 * @author bdmc
 * @date 2022-03-06
 */
@Service
public class TaskWorkTeamServiceImpl extends ServicePlusImpl<TaskWorkTeamMapper, TaskWorkTeam, TaskWorkTeamVo> implements ITaskWorkTeamService {

    @Autowired
    ITaskTeamSpanService spanService;

    @Override
    public TaskWorkTeamVo queryById(Long workTeamId){
        return getVoById(workTeamId);
    }

    @Override
    public TableDataInfo<TaskWorkTeamVo> queryPageList(TaskWorkTeamBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TaskWorkTeam> lqw = buildQueryWrapper(bo);
        Page<TaskWorkTeamVo> result = pageVo(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    @Override
    public TableDataInfo<TaskWorkTeamVo> mySelectPage(TaskWorkTeamBo bo, PageQuery pageQuery) {
        return TableDataInfo.build(baseMapper.mySelectPage(pageQuery.build(),bo));
    }

    @Override
    public List<TaskWorkTeamVo> queryList(TaskWorkTeamBo bo) {
        return baseMapper.mySelectPage(bo);
    }

    private LambdaQueryWrapper<TaskWorkTeam> buildQueryWrapper(TaskWorkTeamBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TaskWorkTeam> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, TaskWorkTeam::getDeptId, bo.getDeptId());
        lqw.like(StringUtils.isNotBlank(bo.getWorkTeamName()), TaskWorkTeam::getWorkTeamName, bo.getWorkTeamName());
        return lqw;
    }

    @Transactional
    @Override
    public Boolean insertByBo(TaskWorkTeamBo bo) {
        TaskWorkTeam add = BeanUtil.toBean(bo, TaskWorkTeam.class);
        validEntityBeforeSave(add);
        boolean flag = save(add);
        if (flag) {
            bo.setWorkTeamId(add.getWorkTeamId());
            for (TaskTeamSpanBo teamSpanBo : bo.getTeamSpanList()) {
                teamSpanBo.setWorkTeamId(add.getWorkTeamId());
                spanService.insertByBo(teamSpanBo);
            }
        }
        return flag;
    }

    @Transactional
    @Override
    public Boolean updateByBo(TaskWorkTeamBo bo) {
        TaskWorkTeam update = BeanUtil.toBean(bo, TaskWorkTeam.class);
        validEntityBeforeSave(update);
        boolean flag = updateById(update);
        if (flag) {
            LambdaQueryWrapper<TaskTeamSpan> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(TaskTeamSpan::getWorkTeamId,update.getWorkTeamId());
            spanService.remove(wrapper);
            for (TaskTeamSpanBo teamSpanBo : bo.getTeamSpanList()) {
                teamSpanBo.setWorkTeamId(update.getWorkTeamId());
                spanService.insertByBo(teamSpanBo);
            }
        }
        return flag;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(TaskWorkTeam entity){
        //TODO 做一些数据校验,如唯一约束
    }


    @Transactional
    @Override
    public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        boolean b = removeByIds(ids);
        LambdaQueryWrapper<TaskTeamSpan> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(TaskTeamSpan::getWorkTeamId,ids);
        spanService.remove(wrapper);
        return b;
    }
}
